home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / nevow / vhost.py < prev    next >
Text File  |  2006-04-14  |  7KB  |  209 lines

  1. # Copyright (c) 2004 Divmod.
  2. # See LICENSE for details.
  3.  
  4. from twisted.python import log
  5. from zope.interface import implements
  6. from nevow import loaders, rend, inevow
  7. from nevow.stan import directive
  8. from nevow import tags
  9.  
  10. class VirtualHostList(rend.Page):
  11.     def __init__(self, nvh):
  12.         rend.Page.__init__(self)
  13.         self.nvh = nvh
  14.  
  15.     stylesheet = """
  16.     body { border: 0; padding: 0; margin: 0; background-color: #efefef; }
  17.     h1 {padding: 0.1em; background-color: #777; color: white; border-bottom: thin white dashed;}
  18. """
  19.  
  20.     def getStyleSheet(self):
  21.         return self.stylesheet
  22.  
  23.     def data_hostlist(self, context, data):
  24.         return self.nvh.hosts.keys()
  25.  
  26.     def render_hostlist(self, context, data):
  27.         host=data
  28.         req = context.locate(inevow.IRequest)
  29.         proto = req.clientproto.split('/')[0].lower()
  30.         port = req.getHeader('host').split(':')[1]
  31.  
  32.         link = "%s://%s" % (proto, host)
  33.  
  34.         if port != 80:
  35.             link += ":%s" % port
  36.  
  37.         link += req.path
  38.  
  39.         return context.tag[tags.a(href=link)[ host ]]
  40.  
  41.     def render_title(self, context, data):
  42.         req = context.locate(inevow.IRequest)
  43.         proto = req.clientproto.split('/')[0].lower()
  44.         host = req.getHeader('host')
  45.         return context.tag[ "Virtual Host Listing for %s://%s" % (proto, host) ]
  46.  
  47.     def render_stylesheet(self, context, data):
  48.         return tags.style(type="text/css")[self.getStyleSheet()]
  49.         
  50.     docFactory = loaders.stan(
  51.         tags.html[
  52.             tags.head[
  53.                 tags.title(render=render_title),
  54.                 tags.directive('stylesheet'),
  55.             ],
  56.             tags.body[
  57.                 tags.h1(render=render_title),
  58.                 tags.ul(data=directive("hostlist"), render=directive("sequence"))[
  59.                     tags.li(pattern="item", render=render_hostlist)]]])
  60.  
  61. class NameVirtualHost(rend.Page):
  62.     """I am a resource which represents named virtual hosts. 
  63.        And these are my obligatory comments
  64.     """
  65.     
  66.     supportNested = True
  67.  
  68.     def __init__(self, default=None, listHosts=True):
  69.         """Initialize. - Do you really need me to tell you that?
  70.         """
  71.         log.msg("Initializing %r" % self)
  72.  
  73.         rend.Page.__init__(self)
  74.         self.hosts = {}
  75.        
  76.         self.default = default
  77.         self.listHosts = listHosts
  78.         
  79.         if self.listHosts and self.default == None:
  80.             self.default = VirtualHostList(self)
  81.             
  82.     def addHost(self, name, resrc):
  83.         """Add a host to this virtual host. - The Fun Stuff(TM)
  84.             
  85.         This associates a host named 'name' with a resource 'resrc'
  86.  
  87.             >>> nvh.addHost('nevow.com', nevowDirectory)
  88.             >>> nvh.addHost('divmod.org', divmodDirectory)
  89.             >>> nvh.addHost('twistedmatrix.com', twistedMatrixDirectory)
  90.  
  91.         I told you that was fun.
  92.         """
  93.         
  94.         self.hosts[name] = resrc
  95.  
  96.     def removeHost(self, name):
  97.         """Remove a host. :(
  98.         """
  99.         del self.hosts[name]
  100.  
  101.     def _getResourceForRequest(self, request):
  102.         """(Internal) Get the appropriate resource for the request
  103.         """
  104.         
  105.         hostHeader = request.getHeader('host')
  106.         
  107.         if hostHeader == None:
  108.             return self.default or rend.NotFound[0]
  109.         else:
  110.             host = hostHeader.split(':')[0].lower()
  111.             
  112.             if self.supportNested:
  113.                 """ If supportNested is True domain prefixes (the stuff up to the first '.')
  114.                     will be chopped off until it's reduced to the tld or a valid domain is 
  115.                     found.
  116.                 """
  117.  
  118.                 while not self.hosts.has_key(host) and len(host.split('.')) > 1:
  119.                     host = '.'.join(host.split('.')[1:])
  120.  
  121.         return (self.hosts.get(host, self.default) or rend.NotFound[0])
  122.  
  123.     def locateChild(self, ctx, segments):
  124.         """It's a NameVirtualHost, do you know where your children are?
  125.         
  126.         This uses locateChild magic so you don't have to mutate the request.
  127.         """
  128.         resrc = self._getResourceForRequest(inevow.IRequest(ctx))
  129.         return resrc, segments
  130.  
  131. class _VHostMonsterResourcePrepathCleaner:
  132.     """VHostMonsterResource cannot modify request.prepath because the
  133.     segments it needs to remove are not appended to prepath until
  134.     *after* it returns the (resource,segments) tuple.
  135.     """
  136.     implements(inevow.IResource)
  137.     def locateChild(self, ctx, segments):
  138.         request = inevow.IRequest(ctx)
  139.         request.prepath = request.prepath[3:]
  140.         return request.site.resource, segments
  141.  
  142. _prepathCleaner = _VHostMonsterResourcePrepathCleaner()
  143.  
  144. class VHostMonsterResource:
  145.     """VHostMonster resource that helps to deploy a Nevow site behind a proxy.
  146.     
  147.     The main problem when deploying behind a proxy is that the scheme, host name
  148.     and port the user typed into their browser are lost because the proxying web
  149.     server forwards the request to something like http://localhost:8080/.
  150.     
  151.     A vhost resource consumes the next 2 segments of the URL to rewrite the
  152.     scheme, host and port in the request object. It then "forwards" the request
  153.     to the site's root resource.
  154.     
  155.     To install the resource use something like:
  156.     
  157.     >>> root = MyRootPage()
  158.     >>> root.putChild('vhost', VHostMonsterResource())
  159.     >>> site = NevowSite(root)
  160.         
  161.     An appropriate Apache configuration using mod_proxy would be::
  162.         
  163.         ProxyPass / http://localhost:8080/vhost/http/real.domainname.com/
  164.  
  165.     If you only want to proxy a part of the url tree, try::
  166.  
  167.         ProxyPass /foo/bar http://localhost:8080/vhost/http/real.domainname.com/foo/bar
  168.  
  169.     Note how the path is equal on both the public and the private server.
  170.     Where as the /vhost/http/real.domainname.com part tells the private
  171.     server the scheme, hostname, and possibly port, the /foo/bar is needed
  172.     at the end to let the private know the path also.
  173.  
  174.     This also means your private server should serve the real content at
  175.     /foo/bar, and not at the root of the tree.
  176.  
  177.     Warning: anyone who can access a VHostMonsterResource can fake the
  178.     host name they are contacting. This can lead to cookie stealing or
  179.     cross site scripting attacks. Never expose /vhost to the Internet.
  180.     """
  181.     implements(inevow.IResource)
  182.  
  183.     def locateChild(self, ctx, segments):
  184.  
  185.         request = inevow.IRequest(ctx)
  186.  
  187.         if len(segments) < 2:
  188.             return rend.NotFound
  189.         else:
  190.             if segments[0] == 'http':
  191.                 request.isSecure = lambda: 0
  192.             elif segments[0] == 'https':
  193.                 request.isSecure = lambda: 1
  194.  
  195.             if ':' in segments[1]:
  196.                 host, port = segments[1].split(':', 1)
  197.                 port = int(port)
  198.             else:
  199.                 host, port = segments[1], 80
  200.            
  201.             request.setHost(host, port)
  202.  
  203.             prefixLen = len('/'+'/'.join(request.prepath)+'/'+'/'.join(segments[:2]))
  204.             request.path = '/'+'/'.join(segments[2:])
  205.             request.uri = request.uri[prefixLen:]
  206.  
  207.             return _prepathCleaner, segments[2:]
  208.  
  209.